<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="/WEB-INF/views/include/taglib.jsp"%>
<script type="text/javascript">
    function page(n,s){
        $("#pageNo").val(n);
        $("#pageSize").val(s);
        $("#searchForm").submit();
        return false;
    }
    $("#contentTable").treeTable();
    var cardNo="";
    var clinicNo="";
    var ordersId="";
    var chargeType="";
    var  costs="";
    var payType="";
    var payMoney="";

    function cardFee(ordersId1){
        ordersId=ordersId1;
        cardNo = readOneCard();
        if (!cardNo) {
            toastr.error("读卡失败");
            return false;
        } else {
            findByOrdersId();
            return false;
        }
    }
    function findByOrdersId(){
        $.ajax({
            type: "post",
            url: "/a/doctor/clinicOrders/findClinicMaster?ordersId=" + ordersId,
            dataType: "json",
            cache: false,
            success: function (data) {
                if(data){
                    clinicNo=data.clinicNo;
                    chargeType=data.chargeType;
                    setCosts();//应收费
                }else{
                    toastr.error("根据ordersId查询门诊号错误！！");
                }
            },
            error: function (data) {
                toastr.error("根据ordersId查询门诊号错误！！");
            }
        });
    }
    //查询余额
    function findBalance(){
        $.ajax({
            type: "get",
            url: "/a/med/medicalCardMaster/findByCardNo?cardNo=" + cardNo,
            dataType: "json",
            cache: false,
            success: function (data) {
                if(!data){
                    toastr.error("根据卡号读取信息错误！");
                }else{
                    var balance=data.balance;
                    if(balance<costs){
                        toastr.error("一卡通余额不足，余额为："+balance+"应付款为："+costs);
                        return false;
                    }else{
                        saveFee();
                    }
                }
            },
            error: function (data) {
                toastr.error("根据卡号读取信息错误！");
            }
        });
    }
    /**
     * 计算优惠
     */
    function setCosts(){
        $.ajax({
            type: "post",
            url: "/a/finance/outpRcptMaster/setCosts?clinicNo="+clinicNo+"&chargeType="+chargeType+"&ordersId="+ordersId,
            dataType: "json",
            cache: false,
            success: function (data) {
                if(data.code="success"){
                    costs=data.data;
                    findBalance();
                }
            },
            error: function (data) {
                toastr.error("网络连接错误");
            }
        });
    }
    /**
     * 陈晓阳
     * @param cardNo
     * @param totalCharges
     * @param totalMoney
     */
    function saveFee(){
        var cardPayFlag=1;
        payType="3"
        payMoney=costs;
        var data={cardPayFlag:cardPayFlag,payType:payType,payMoney:payMoney,cardNo:cardNo,costs:costs,orderIds:ordersId,totalMoney:costs};
        console.info(data);
        $.ajax({
            type: 'post',
            url: '${ctx}/finance/outpRcptMaster/save',
            data:data,
            cache:false,
            dataType:"json",
            success: function (data) {
                if (data.code == 'success') {
                    toastr.success("收费成功");
                    var pdata={consumeMoney:costs,cardNo:cardNo};
                    printPdf('${ctx}/med/medicalCardMaster/printItem',pdata);
                    loadByUrl(timestamp("${ctx}/lab/labApply/waitFeeData"));
                    return;
                } else {
                    toastr.error("收费失败");
                    return;
                }
            }
        });
    }
 </script>
<div class="content-wrap">
    <div class="wrapper" style="bottom: 50px;">
        <ul class="nav nav-tabs">
            <li>
                <a href="#" onclick="loadByUrl('${ctx}/lab/labApply/labConfirm')">检验确认列表</a>
            </li>
            <li class="active">
                <a href="#">检验诊间收费</a>
            </li>
        </ul>
        <section class="panel panel-default">
            <div class="panel-body">
                <div class="table-responsive no-border">
                    <div class="datatable-top">
                        <div class="pull-left">
                            <form:form id="searchForm" modelAttribute="labApply"  onsubmit="return loadDivForm('mainCenterDiv','searchForm','${ctx}/lab/labApply/waitFeeData');" method="post" class="form-inline">
                                <input id="pageNo" name="pageNo" type="hidden" value="${page.pageNo}"/>
                                <input id="pageSize" name="pageSize" type="hidden" value="${page.pageSize}"/>
                                <input id="pageNo" name="pageNo" type="hidden" value="${page.pageNo}"/>
                                <input id="pageSize" name="pageSize" type="hidden" value="${page.pageSize}"/>
                                 <label>申请科室 ：</label><sys:treeselect selectedParent="false" id="orgDeptId" name="billingDept"  value="${labApply.billingDept}"  update="false" labelName="implementDeptEt.deptName" labelValue="${labApply.implementDeptEt.deptName}"
                                                                      title="科室" url="/sys/orgDept/treeData" extId="" cssClass="form-control" />
                                <label>姓名：</label><form:input path="name"  htmlEscape="false" class="form-control" />
                                <input id="search" class="btn btn-primary" type="submit" value="查询"/>&nbsp;&nbsp;

                            </form:form>

                        </div>
                    </div>
                    <table id="contentTable" class="table table-bordered mg-t datatable">
                        <thead>
                        <tr>
                            <th>姓名</th>
                            <th>申请时间</th>
                            <th>申请序号</th>
                            <th>申请科室</th>
                            <th>执行科室</th>
                            <th>收费标识</th>
                            <th>申请医生</th>
                            <th>操作</th>
                        </tr>
                        </thead>
                        <tbody >
                        <c:forEach items="${page.list}" var="lab" varStatus="ss">
                            <tr id="${lab.id}" >
                                <td>${lab.patMasterIndex.name}</td>
                                <td> <fmt:formatDate value="${lab.createDate}" pattern="yyyy-MM-dd HH:mm:ss"></fmt:formatDate> </td>
                                <td> ${lab.applyOn}</td>
                                <td> ${lab.billingDeptEt.deptName}</td>
                                <td> ${lab.implementDeptEt.deptName}</td>
                                <td>${fns:getDictLabels(lab.chargeFlag,'NO_CHARGE','')}</td>
                                <td> ${lab.createBy.name}</td>
                                <td>
                                    <a href="#"  class="applyBt btn btn-danger btn-xs" onclick="cardFee('${lab.ordersId}')">读卡收费</a>
                                </td>
                            </tr>
                            <c:if test="${lab.labItemList.size()>0}">
                                <tr id="${ss.count}" pid="${lab.id}">
                                    <td colspan="9">
                                        <table id="printbtn" class="table table-bordered mg-t  ">
                                            <tr>
                                                <th>序号</th>
                                                <th>项目名称</th>
                                                <th>项目编码</th>
                                                <th>费用</th>
                                             </tr>
                                            <c:forEach items="${lab.labItemList}" var="item" varStatus="ss">
                                                <tr>
                                                    <td> ${ss.count}</td>
                                                    <td> ${item.itemName}</td>
                                                    <td> ${item.itemCode}</td>
                                                    <td> ${item.costs}</td>
                                                </tr>
                                            </c:forEach>
                                        </table>
                                    </td>
                                </tr>
                            </c:if>
                        </c:forEach>
                    </table>
                    <div class="pagination">${page}</div>
                </div>
            </div>
        </section>
        <input type="hidden" id="radioVal" value="${labApply.inoroutFlag}">
     </div>
</div>